<?php

namespace app\common\dao\work;

use app\common\dao\BaseDao;
use app\common\model\work\WorkReportModel;

class WorkReportDao extends BaseDao
{

    /**
     * @return WorkReportModel
     */
    protected function getModel(): string
    {
        return WorkReportModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {

        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['firm_id']) && $where['firm_id'] !== '', function ($query) use ($where) {
                $query->where('firm_id', intval($where['firm_id']));
            })
            ->when(isset($where['user_id']) && $where['user_id'] !== '', function ($query) use ($where) {
                $query->where('user_id', intval($where['user_id']));
            })
            ->when(isset($where['report_type']) && $where['report_type'] !== '', function ($query) use ($where) {
                $query->where('report_type', $where['report_type']);
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('remark|config_name', '%' . trim($where['keywords']) . '%');
            });
    }
}